package com.example.xxcjgl.utils

// Extensions.kt String扩展
fun String.isValidUsername(): Boolean {
    return this.isNotBlank() && this.length in 3..20
}

fun String.isValidPassword(): Boolean {
    return this.length >= 6
}

fun String.isValidScore(): Boolean {
    val score = this.toFloatOrNull()
    return score != null && score in 0f..150f
}

fun String.toPinyinInitials(): String {
    // 简化的拼音首字母转换（实际项目应使用拼音库）
    return if (this.isNotEmpty()) this.first().toString().uppercase() else ""
}

// Float扩展
fun Float?.formatScore(): String {
    return this?.toString() ?: "无"
}

// Date扩展
fun String.toDisplayDate(): String {
    return try {
        val inputFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        val outputFormat = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
        val date = inputFormat.parse(this)
        outputFormat.format(date ?: Date())
    } catch (e: Exception) {
        this
    }
}

// Compose Modifier扩展
fun Modifier.conditional(condition: Boolean, modifier: Modifier.() -> Modifier): Modifier {
    return if (condition) {
        then(modifier(Modifier))
    } else {
        this
    }
}